<template>
    <div class="px-8">
        <com-filterTable
            :ref="pageConfig.key"
            class=""
            :services="getMedicalVisits"
            :filter-form-config="pageConfig.formConfig"
            :filterInline="false"
            :params="{
                ...pageConfig.params
            }"
            :filterCol="4"
            filterLabelWidth="90px"
            :table-border="true"
            :showFormFilter="true"
            :showRequestNum="false"
            :top-operation="pageConfig.topOperation"
            :table-column="pageConfig.columnConfig"
            :queryDataHandle="queryDataHandle"
            :dataFormatHandle="dataFormatHandle"
            :autoLoad="false"
        >
        </com-filterTable>

        <com-formDialog
            ref="dialog"
            v-bind="dialogData"
            @changeOut="changeOut"
            @cancel="dialogCancelHandle"
            @submit="submitHandle"
        />
    </div>
</template>

<script>
import { formItemMap } from "@utils/formField";
import service from "@service";
import _ from "lodash";
import {getMedicalVisits} from "@/service/api";
export default {
    name: "medicalVisit",
    data() {
        return {
            getMedicalVisits,
            activeName: "medicalVisit",
            oldFormData: null,
            comDialogData: {},
            batchSelected: [],
            pageConfig: {
                key: "medicalVisit",
                serviceName: "medicalVisit",
                label: "就诊信息查询",
                params: {},
                formConfig: [
                    {key: 'deptName', label: '科室', required: true},
                ],
                columnConfig: [
                    {  "value": "visitId", "label": "就诊id", },
                    {  "value": "patientName", "label": "患者", },
                    {  "value": "gender", "label": "性别", },
                    {  "value": "age", "label": "年龄", },
                    {  "value": "visitDeptName", "label": "就诊科室", },
                    {  "value": "visitDoctorName", "label": "医生", },
                    {  "value": "visitDate", "label": "就诊时间",width: 180, },
                    {  "value": "action", "label": "操作", type: 'render', render: (h, {data: {row}}) => {
                        const tableColumn = [
                            {  "value": "diagnosisId", "label": "诊断ID", },
                            {  "value": "diagnosisDoctorName", "label": "就诊医生", },
                            {  "value": "diagnosisName", "label": "诊断名称", },
                            {  "value": "mainSymptom", "label": "症状", },
                            {  "value": "createTime", "label": "就诊日期", width: 180,},

                       ];
                        const openDetail=() => {
                            this.comDialogData = {
                                    submitHandle: (data) => {
                                        return new Promise((resolve) => {
                                            setTimeout(resolve(true) , 1000)
                                        })
                                    },
                                    configs: [
                                        {
                                            key: 'diagnoses',
                                            label: '',
                                            noLabel: true,
                                            type: 'render',
                                            render: (h) => {
                                                return <com-table pagination={false} import-data={row.diagnoses} table-column={tableColumn}></com-table>
                                            }
                                        }
                                    ],
                                    title: "诊断记录",
                                    width: "850px",
                                    height: "500px",
                                    labelPosition: "left",
                                    labelWidth: "90px",
                                    col: 1,
                                };
                                this.$refs.dialog.show();
                        }
                        return <el-button type='text' on-click={openDetail}>诊断记录</el-button>
                    } },

                ],
                topOperation: {
                    left: [
                        // {
                        //     name: "选中",
                        //     key: "add",
                        //     type: "primary",
                        //     requireArguments: ["batchSelected"],
                        //     handle: (batchSelected) => {
                        //         console.log(batchSelected, "选中的表格数据！")
                        //         this.comDialogData = {
                        //             submitHandle: (data) => {
                        //                 return new Promise((resolve) => {
                        //                     setTimeout(resolve(true) , 1000)
                        //                 })
                        //             },
                        //             configs: [
                        //                 {
                        //                     key: 'key1',
                        //                     label: '输入值',
                        //                     required: true
                        //                 }
                        //             ],
                        //             batchSelected: batchSelected,
                        //             title: "休眠",
                        //             width: "750px",
                        //             height: "200px",
                        //             labelPosition: "left",
                        //             labelWidth: "90px",
                        //             col: 1,
                        //         };
                        //         this.$refs.dialog.show();
                        //     },
                        // },
                        // {
                        //     name: "查询表单",
                        //     key: "delete",
                        //     type: "primary",
                        //     requireArguments: ["filterForm"],
                        //     handle: (filterForm) => {
                        //         console.log(filterForm, "查询表单")
                        //     },
                        // },
                    ],
                },
            },
        };
    },
    watch: {},
    computed: {
        dialogData() {
            let { comDialogData } = this;
            return {
                configs:
                    this[comDialogData.configName] || this.formDialogConfig,
                ...comDialogData,
            };
        },
        formDialogConfig() {
            return [];
        },
    },
    mounted() {},
    methods: {

        changeOut({ key, formData } = {}) {
            this.oldFormData = { ...formData };
        },
        dataFormatHandle(data) {

            return data.map(item => {
                let out = {
                    ...item,
                    ...item.medicalVisit,
                    ...item.patient
                }
                return out;
            });

        },
        queryDataHandle(params) {
            let temp = {
                ...params,
            };
            return temp;
        },
        loadList() {
            let ref = this.$refs[this.activeName];
            if(Array.isArray(ref)) {
                ref = ref[0];
            }

            ref && ref.filterFormQueryHandle();
        },
        dialogCancelHandle() {
            this.$refs.dialog.close();
        },
        async submitHandle(data) {
            let handle = this.dialogData.submitHandle;
            handle = typeof handle !== 'function' ? this[handle] : handle;
            if (handle) {
                let loading = await this.$store.dispatch("global/loading");
                handle(data)
                    .then((res) => {
                        loading.close();
                        this.$refs.dialog.close();
                        this.loadList();
                    })
                    .catch((e) => {
                        loading.close();
                    });
            }
        },
        // 休眠处理函数
        claimOrderHandle(data) {
            const selected = this.comDialogData.batchSelected;
            return service('sleep', {
                data: {
                    ...data,
                    "problemProcessIds":(selected||[]).map(item => item.wrkfmId).join(','),
                    "provCode": this.provCode,
                    "staffId": this.staffId,
                }
            })
        }
    },
};
</script>
